﻿using FileHelpers;
using SharpStockLib.DataAccess.CSVMappers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using SharpStockLib.Data;

namespace SharpStockTests
{
    
    
    /// <summary>
    ///This is a test class for YahooStockHistoricalTest and is intended
    ///to contain all YahooStockHistoricalTest Unit Tests
    ///</summary>
    [TestClass()]
    public class YahooStockHistoricalTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///Tests the parsing of a CSV provided by Yahoo's historical quotes service.
        ///</summary>
        [TestMethod()]
        public void YahooStockHistoricalParseTest()
        {
            string csvDirectory = testContextInstance.DeploymentDirectory;
            const string fileName = "INTC.csv";

            FileHelperEngine engine = new FileHelperEngine(typeof(YahooStockHistorical));
            YahooStockHistorical[] quotes = engine.ReadFile(csvDirectory + "\\" + fileName) as YahooStockHistorical[];

            Assert.IsNotNull(quotes);
            Assert.AreEqual(quotes.Length, 3449);
        }

        /// <summary>
        ///A test for ToIData
        ///</summary>
        [TestMethod()]
        public void ToIDataTest()
        {
            SimpleData expected = new SimpleData(30.62, 31.00, 29.50, 29.50, 187600, new DateTime(2000, 6, 12));

            string csvDirectory = testContextInstance.DeploymentDirectory;
            const string fileName = "APOL.csv";

            FileHelperEngine engine = new FileHelperEngine(typeof(YahooStockHistorical));
            YahooStockHistorical[] quotes = engine.ReadFile(csvDirectory + "\\" + fileName) as YahooStockHistorical[];
            Assert.IsNotNull(quotes);

            // Casts to SimpleData because comparing two IDatas' equality will result in testing the references' equality, which will fail obviously
            SimpleData actual = (SimpleData)quotes[0].ToIData();

            Assert.IsTrue(expected.Equals(actual));
        }
    }
}
